<?php

/**
 * GovOrganization form.
 *
 * @package    erp-dnict
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class GovOrganizationForm extends BaseGovOrganizationForm
{
  public function configure()
  {
    $this->setWidgets(array(
        'name'                  =>  new sfWidgetFormInput(),
        'description'           =>  new sfWidgetFormTextarea(),
        'parent_id'				=>	new sfWidgetFormSelect(array('choices' => $this->getParentList())),
        'is_active'             =>  new sfWidgetFormInputCheckbox(),
    ));

    $this->widgetSchema->setLabels(array(
        'name'                  =>  'Tên phòng ban:*',
        'description'           =>  'Ghi chú:',
        'parent_id'				=>	'Phòng cấp trên',
        'is_active'             =>  'Khả dụng:',
    ));

    $this->setValidators(array(
        'name'                  =>  new sfValidatorString(array('required' => true)),
        'description'           =>  new sfValidatorString(array('required' => false)),
        'parent_id'				=>	new sfValidatorChoice(array('required' => false, 'choices' => array_keys($this->getParentList()))),
        'is_active'             =>  new sfValidatorBoolean(),
    ));
    $this->widgetSchema->setNameFormat('gov_organization[%s]');
  }

  private function getParentList()
  {
  	$q = Doctrine_Query::create()
  			->select("go.id, go.name")
  			->from('GovOrganization go')
  			->where('go.is_active = ?', true);
  	$results = $q->execute();
  	$item = array();
  	$item[0] = "Không có";
  	foreach ($results as $result) {
  		$item[$result['id']] = $result['name'];
  	}
  	return $item;
  }
}
